package com.bootdo.lvyou.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ModelAttribute;
import com.bootdo.common.controller.BaseController;
import com.bootdo.lvyou.domain.XcxConfig;
import com.bootdo.lvyou.service.XcxConfigService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 小程序公共参数配置
 * 
 * @author haojiangbo
 * @email 2100272728@qq.com
 * @date 2019-10-14 16:42:32
 */
 
@Controller
@RequestMapping("/lvyou/xcxConfig")
public class XcxConfigController extends BaseController{


	@Autowired
	private XcxConfigService xcxConfigService;
	
	@ModelAttribute
	public XcxConfig get(@RequestParam(required=false) Long id) {
		if (id != null){
			return xcxConfigService.get(id);
		}else{
			return new XcxConfig();
		}
	}


	
	//insert into sys_menu(parent_id,name,url,perms,type)  values (149,'测试','/lvyou/xcxConfig','lvyou:xcxConfig:xcxConfig',1)
	//insert into sys_menu(parent_id,name,url,perms,type)  values (160,'修改','','lvyou:xcxConfig:edit',2)
	//type 0是分类菜单 1是小菜但 2是按钮
	@GetMapping()
	@RequiresPermissions("lvyou:xcxConfig:xcxConfig")	
	String XcxConfig(XcxConfig xcxConfig,HttpServletRequest request,HttpServletResponse response){
	    return "lvyou/xcxConfig/xcxConfig";
	}
	
	@ResponseBody
	@PostMapping("/list")
	@RequiresPermissions("lvyou:xcxConfig:xcxConfig")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
        if(!isAdmin()){
        	query.put("userId", getUserId());
		}
		List<XcxConfig> xcxConfigList = xcxConfigService.list(query);
		int total = xcxConfigService.count(query);
		PageUtils pageUtils = new PageUtils(xcxConfigList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("lvyou:xcxConfig:edit")
	String add(XcxConfig xcxConfig,HttpServletRequest request,HttpServletResponse response){
	    return "lvyou/xcxConfig/xcxConfigedit";
	}

	@PostMapping("/getById")
	@ResponseBody
	R getById(XcxConfig xcxConfig) {
		return R.ok().put("i",xcxConfig);
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("lvyou:xcxConfig:edit")
	public R save( XcxConfig xcxConfig){
		if(!isAdmin()){
			if(null == xcxConfig.getUserId()){
				xcxConfig.setUserId(getUserId());
				xcxConfig.setUserName(getUsername());
			}
		}
		if(xcxConfigService.save(xcxConfig)>0){
			return R.ok();
		}
		return R.error();
	}
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("lvyou:xcxConfig:edit")
	public R remove( Long id){
		if(xcxConfigService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("lvyou:xcxConfig:edit")
	public R remove(@RequestParam("ids[]") Long[] ids){
		xcxConfigService.batchRemove(ids);
		return R.ok();
	}
	
}